var WasteSetting = function () {
  var template = /* html */ `
    <div class="waste-setting">
      <div style="margin-bottom:10px" v-if="type!=='approval'">
        <el-button size="medium" type="primary" @click="addWaste">新增损耗数据</el-button>
        <el-button size="medium" type="primary" @click="ensure">确定</el-button>
      </div>
      <div class="waste-setting-table-div">
        <el-table fit stripe border :data="wasteRateList" style="width: 100%" height="parent">
          <el-table-column label="订单数" min-width="300px" column-key="orderTotalRange" key="orderTotalRange" align="center">
            <template slot-scope="scope">
              <span v-if="type==='approval'">{{scope.row.orderTotalStart}} ~ {{scope.row.orderTotalEnd}}</span>
              <template v-else>
              <el-input-number v-model="scope.row.orderTotalStart" :controls="false" :min="1" :max="scope.row.orderTotalEnd" :precision="0"
                class="waste-setting-number" size="mini">
              </el-input-number>
              <span>~</span>
              <el-input-number v-model="scope.row.orderTotalEnd" :controls="false" :min="scope.row.orderTotalStart?scope.row.orderTotalStart:1"
                :precision="0" class="waste-setting-number" size="mini">
              </el-input-number>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="损耗率" min-width="180px" column-key="wasteRate" key="wasteRate" align="center">
            <template slot-scope="scope">
              <span v-if="type==='approval'">{{scope.row.wasteRate}}%</span>
              <template v-else>
              <el-input-number is-decimal-zero v-model="scope.row.wasteRate" :controls="false" :min="0" :precision="1" size="mini"
                style="width:calc(100% - 20px)">
              </el-input-number>
              <span>%</span>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="损耗数量" min-width="180px" column-key="wasteTotal" key="wasteTotal" align="center">
            <template slot-scope="scope">
              <span v-if="type==='approval'">{{scope.row.wasteTotal}}</span>
              <el-input-number v-else is-decimal-zero v-model="scope.row.wasteTotal" :controls="false" :min="0" :precision="3" size="mini"
                style="width:100%">
              </el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" width="80px" column-key="handle" key="handle" align="center" v-if="type!=='approval'">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="delWaste(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  `;

  return {
    props: {
      bomForm: {
        type: Object,
        required: true
      },
      type: {
        type: String,
        default: 'add'
      }
    },

    data: function () {
      var wasteRateList = this.bomForm.wasteRateList;
      wasteRateList = _.orderBy(wasteRateList, ['orderTotalStart'], ['asc']);
      return {
        wasteRateList: _.cloneDeep(wasteRateList)
      };
    },

    methods: {
      addWaste: function () {
        var wasteRateList = this.wasteRateList;
        wasteRateList.push({
          id: SysUtil.getId(),
          orderTotalStart: undefined,
          orderTotalEnd: undefined,
          wasteRate: undefined,
          wasteTotal: undefined
        });
      },

      delWaste: function (wasteRate) {
        var wasteRateList = this.wasteRateList;
        var index = _.findIndex(wasteRateList, {
          id: wasteRate.id
        });
        if (index !== -1) {
          wasteRateList.splice(index, 1);
        }
      },

      ensure: function () {
        var code = this.checkValid();
        if (code !== '') {
          var msg = i18n.getMsg(`bomBusiness.bomMaterialSetting.error.${code}`);
          this.$alert(msg, '信息');
          return;
        }

        var wasteRateList = this.wasteRateList;
        this.bomForm.wasteRateList = wasteRateList;
        this.$emit('setting-success', wasteRateList);
      },

      checkValid: function () {
        var wasteRateList = this.wasteRateList;
        var fields = ['orderTotalStart', 'orderTotalEnd', 'wasteRate', 'wasteTotal'];
        for (var i = 0; i < wasteRateList.length; i++) {
          var waste = wasteRateList[i];
          for (var j = 0; j < fields.length; j++) {
            var field = fields[j];
            if (waste[field] === undefined) {
              return field;
            }
          }
        }

        for (var i = 0; i < wasteRateList.length - 1; i++) {
          var wasteRate1 = wasteRateList[i];
          for (var j = i + 1; j < wasteRateList.length; j++) {
            var wasteRate2 = wasteRateList[j];
            if (this.checkOverlap(wasteRate1, wasteRate2)) {
              return 'hasOverlap';
            }
          }
        }

        return '';
      },

      checkOverlap: function (obj1, obj2) {
        var startValue = Math.max(obj1.orderTotalStart, obj2.orderTotalStart);
        var endValue = Math.min(obj1.orderTotalEnd, obj2.orderTotalEnd);
        if (startValue <= endValue) {
          return true;
        }
        return false;
      }
    },

    template: template
  };
}();
